# 修复数据库只读问题的 PowerShell 脚本

Write-Host "正在检查数据库连接 info." -ForegroundColor Yellow

# 提示用户输入 MySQL root 密码
$password = Read-Host "请输入 MySQL root 密码（直接回车如果没有密码）"

# 构建 MySQL 命令
if ([string]::IsNullOrWhiteSpace($password)) {
    $mysqlCmd = "mysql -u root"
} else {
    $mysqlCmd = "mysql -u root -p$password"
}

Write-Host "`n正在授予数据库权限..." -ForegroundColor Green

# 授予权限的 SQL 命令
$sqlCommands = @"
GRANT ALL PRIVILEGES ON new_backend_project.* TO 'root'@'localhost';
FLUSH PRIVILEGES;
SELECT @@read_only as 'Read Only Status';
"@

# 执行 SQL 命令
$sqlCommands | & cmd /c "$mysqlCmd 2>&1"

Write-Host "`n完成！现在请运行以下命令尝试迁移：" -ForegroundColor Green
Write-Host "php artisan migrate" -ForegroundColor Cyan

